package org.jeecg.modules.fw.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.sf.saxon.expr.instruct.ForEach;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.modules.business.entity.BusHotel;
import org.jeecg.modules.business.service.IBusHotelService;
import org.jeecg.modules.fw.entity.FwLivingJx;
import org.jeecg.modules.fw.mapper.FwLivingJxMapper;
import org.jeecg.modules.fw.service.IFwLivingJxService;
import org.jeecg.modules.rooms.entity.CesAllDayPriceRule;
import org.jeecg.modules.rooms.entity.CesRooms;
import org.jeecg.modules.rooms.service.CesRoomsServiceImpl;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.service.impl.SysBaseApiImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Description: fw_living_jx
 * @Author: jeecg-boot
 * @Date:   2023-06-01
 * @Version: V1.0
 */
@Service
public class FwLivingJxServiceImpl extends ServiceImpl<FwLivingJxMapper, FwLivingJx> implements IFwLivingJxService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private CesRoomsServiceImpl cesRoomsService;
    @Resource
    private SysBaseApiImpl sysBaseAPI;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private IBusHotelService busHotelService;

    @Override
    public void addJxCache(FwLivingJx fwLivingJx) {
        Calendar calendar = Calendar.getInstance();
        Calendar jxTimeCalendar = Calendar.getInstance();
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        try {
            Date time = timeFormat.parse(fwLivingJx.getJxTime());
            jxTimeCalendar.setTime(time);
        } catch (ParseException ex) {
            throw new RuntimeException(ex);
        }
        calendar.setTime(fwLivingJx.getJxDate());
        calendar.set(Calendar.HOUR, jxTimeCalendar.get(Calendar.HOUR));
        calendar.set(Calendar.MINUTE, jxTimeCalendar.get(Calendar.MINUTE)-2);
        redisTemplate.opsForZSet().add("jxLiving", fwLivingJx.getId(), new Date().getTime());
    }

    @Override
    public void addJxMess(String hotel) {
        Set<Object> set = redisTemplate.opsForZSet().rangeByScore("jxLiving", 0, Calendar.getInstance().getTimeInMillis());
        if (CollUtil.isEmpty(set)){
            return;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        set.forEach(e -> {
            FwLivingJx livingJx = getById((Serializable) e);
            if (livingJx == null){
                return;
            }
            List<CesRooms> cesRooms = cesRoomsService.list(Wrappers.<CesRooms>lambdaQuery().eq(CesRooms::getLivingOrderId, livingJx.getLivingOrderId()));
            if (!cesRooms.isEmpty()){
                /*添加消息通知*/
                BusHotel busHotel = busHotelService.getById(cesRooms.get(0).getHotelId());
                LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(SysUser::getDelFlag, 0);
                lambdaQueryWrapper.eq(SysUser::getRelTenantIds, busHotel.getTenantId());
                List<SysUser> sysUsers = sysUserService.list(lambdaQueryWrapper);
                String userIds = "";
                for (SysUser sysUser : sysUsers) {
                    userIds += sysUser.getUsername() + ",";
                }
                if (!userIds.isEmpty()) {
                    MessageDTO messageDTO = new MessageDTO();
                    messageDTO.setFromUser("");
                    messageDTO.setToUser(userIds);
                    messageDTO.setTenantId(busHotel.getTenantId());
                    messageDTO.setTitle(cesRooms.get(0).getPrefix() + cesRooms.get(0).getName() + "房间需要被叫醒-" + format.format(livingJx.getJxDate()) + " " + livingJx.getJxTime());
                    messageDTO.setContent(cesRooms.get(0).getPrefix() + cesRooms.get(0).getName() + "房间需要被叫醒-" + format.format(livingJx.getJxDate()) + " " + livingJx.getJxTime());
                    messageDTO.setCategory(CommonConstant.MSG_CATEGORY_1);
                    messageDTO.setMsgCategoryDetail(CommonConstant.MSG_ROOM);
                    sysBaseAPI.sendSysAnnouncement(messageDTO);
                }
            }
            redisTemplate.opsForZSet().remove("jxLiving", livingJx.getId());
        });
    }
}
